package com.wildwolf.project.wildwolf.service;

import android.net.Uri;
import android.os.RemoteException;

import com.wildwolf.project.wildwolf.WildWolfService;
import com.wildwolf.project.wildwolf.model.UserInfo;
import com.wildwolf.project.wildwolf.service.aidl.IChatManager;
import com.wildwolf.project.wildwolf.service.aidl.IPrivacyListManager;
import com.wildwolf.project.wildwolf.service.aidl.IRoster;
import com.wildwolf.project.wildwolf.service.aidl.IXmppConnection;
import com.wildwolf.project.wildwolf.service.aidl.IXmppFacade;
import com.wildwolf.project.wildwolf.utils.PresenceType;

import org.jivesoftware.smack.packet.Presence;

/**
 * XMPP外部接口 WildWolfService.
 * @author LEON
 */
public class XmppFacade extends IXmppFacade.Stub {

    private XmppConnectionAdapter xmppConnectionAdapter;
    /**
     * 
     */
    private final WildWolfService service;

    /**
     * 构造一个XMPP外部接口
     *
     * @param service  
     */
    public XmppFacade(final WildWolfService service) {
    	this.service = service;
    }
/**
 * 改变当前的状态
 */
    @Override
    public void changeStatus(int status, String msg) {
    	initConnection();
	xmppConnectionAdapter.changeStatus(status, msg);
    }

    /**
     *异步连接
     */
    @Override
    public void connectAsync() throws RemoteException {
    	initConnection();
	xmppConnectionAdapter.connectAsync();
    }

   /**
    * 同步连接
    */
    @Override
    public void connectSync() throws RemoteException {
    	initConnection();
	xmppConnectionAdapter.connectSync();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public IXmppConnection createConnection() throws RemoteException {
    	initConnection();
	return xmppConnectionAdapter;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void disconnect() throws RemoteException {
    	initConnection();
	xmppConnectionAdapter.disconnect();
    }

    /**
     * 获取聊天管理器
     */
    @Override
    public IChatManager getChatManager() throws RemoteException {
    	initConnection();
	return xmppConnectionAdapter.getChatManager();
    }

    /**
     * 获取好友列表
     */
    @Override
    public IRoster getRoster() throws RemoteException {
    	initConnection();
	return xmppConnectionAdapter.getRoster();
    }

    /**
     * 获取隐私管理器
     */
    @Override
    public IPrivacyListManager getPrivacyListManager() {
    	initConnection();
	return xmppConnectionAdapter.getPrivacyListManager();
    }
/**
 * 发送状态数据包
 */
    @Override
    public void sendPresencePacket(PresenceAdapter presence) throws RemoteException {
    	initConnection();
	Presence _presence = new Presence(PresenceType.getPresenceTypeFrom(presence.getType()));
	_presence.setTo(presence.getTo());
	xmppConnectionAdapter.getAdaptee().sendPacket(_presence);
    }

    @Override
    public void call(String jid) throws RemoteException {
    }
/**
 * 发布我的头像通知
 */
    @Override
    public boolean publishAvatar(Uri avatarUri) throws RemoteException {
    	initConnection();
	WildwolfAvatarManager wildwolfAvatarManager = xmppConnectionAdapter.getAvatarManager();
	if (wildwolfAvatarManager == null)
	    return false;

	return wildwolfAvatarManager.publishAvatar(avatarUri);
    }
/**
 * 禁用头像发布通知
 */
    @Override
    public void disableAvatarPublishing() throws RemoteException {
    	initConnection();
	WildwolfAvatarManager wildwolfAvatarManager = xmppConnectionAdapter.getAvatarManager();
	if (wildwolfAvatarManager != null)
		wildwolfAvatarManager.disableAvatarPublishing();
    }
/**
 * 返回当前用户的信息
 */
    @Override
    public UserInfo getUserInfo() throws RemoteException {
    	initConnection();
	return xmppConnectionAdapter.getUserInfo();
    }
    /**
     * 初始化创建一个连接
     */
    private void initConnection() {
	if (xmppConnectionAdapter == null) {
	    xmppConnectionAdapter = service.createConnection();
	}
    }
}
